﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Common.Configuration;
using ForzaGT3.Areas.Admin.Models;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class CircuitController : Controller
    {
        //
        // GET: /Admin/Circuit/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Name", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Circuit> data = Circuit.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Circuit";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(CircuitModel model)
        {
            
            if (ModelState.IsValid)
            {
                Circuit data = new Circuit();

                data.CountryId = model.CountryId;
                data.Name = model.Name;
                data.Remarks = model.CircuitInfo;
                data.Longitude = model.Longitude;
                data.Latitude = model.Latitude;
                data.CircuitId = Guid.NewGuid();
                data.CreatedBy = HttpContext.User.Identity.Name;
                data.CreatedDate = DateTime.Now;

                if (model.Image != null)
                {
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.CircuitImageFolder) + model.Image.FileName);
                    data.ImageUrl = SiteSettings.CircuitImageFolder + model.Image.FileName;
                }

                data.Insert();
                return RedirectToAction(MVC.Admin.Circuit.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            Circuit data = Circuit.GetById(Id);
            CircuitModel model = new CircuitModel();
            model.Id = data.CircuitId;
            model.CountryId = data.CountryId;
            model.Name = data.Name;
            model.Latitude = data.Latitude;
            model.Longitude = data.Longitude;
            model.CircuitInfo = data.Remarks;
            model.ImageUrl = data.ImageUrl;
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(CircuitModel model)
        {
            Circuit data = Circuit.GetById(model.Id);
            if (data != null)
            {
                data.ModifiedBy = HttpContext.User.Identity.Name;
                data.ModifiedDate = DateTime.Now;
                data.Name = model.Name;
                data.Remarks = model.CircuitInfo;
                data.Longitude = model.Longitude;
                data.Latitude = model.Latitude;
                data.CountryId = model.CountryId;

                if (model.Image != null)
                {
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.CircuitImageFolder) + model.Image.FileName);
                    data.ImageUrl = SiteSettings.CircuitImageFolder + model.Image.FileName;
                }

                
                data.Update();

                return RedirectToAction(MVC.Admin.Circuit.Index());
            }

            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Circuit data = Circuit.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }

            return RedirectToAction(MVC.Admin.Circuit.Index());
        }

    }
}
